package com.pengtu.web.asset.message;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.app.Attachment;
import com.pengtu.entity.app.User;
import com.pengtu.entity.asset.Message.News;
import com.pengtu.security.SpringSecurityUtils;
import com.pengtu.service.app.SystemManager;
import com.pengtu.service.asset.MessageManager;
import com.pengtu.utils.DateUtils;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

@Namespace("/asset/message")
@Result(name = BaseActionSupport.RELOAD, location = "news.action", type = "redirect")
/*
 * @InterceptorRefs({
 * 
 * @InterceptorRef(value="fileUpload",params={"AllowedExtensions","jpg,gif,doc,txt"
 * ,"MaximumSize","1048576"}),
 * 
 * @InterceptorRef("defaultStack") })
 */
public class NewsAction extends BaseActionSupport<News> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private MessageManager messageManager;
	private SystemManager systemManager;

	private String id;
	private News news;
	private Page<News> page = new Page<News>(10);
	private User user;
	private Attachment attachmention;
	
	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("createDate");
			page.setOrder(Page.DESC);
		}
		page = messageManager.findNews(page, queryFilter);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		user = SpringSecurityUtils.getCurrentUser();
		return INPUT;
	}

	public News getNews() {
		return news;
	}
	public void setNews(News news) {
		this.news = news;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub

		// 保存发布者
		if (StringUtils.isEmpty(id)) {
			news.setPublishPerson((User) SpringSecurityUtils.getCurrentUser());
		} else {
			news.setPublishPerson(news.getPublishPerson());
		}
		messageManager.saveOrUpdateNews(news);
		addActionMessage("Title为" + news.getTitle() + "的新闻添加成功Q@!");
		if (null != attachmention) {
			attachmention.setTableId(news.getId());
			attachmention.setTableField("news_attachment");
			attachmention.setTableName("news");
			systemManager.uploadFile(Struts2Utils.getAppRootPath(),
					attachmention);

			news.setAttachment(attachmention);
			messageManager.saveOrUpdateNews(news);
			String mes = "";
			if(StringUtils.isNotEmpty(id)){
				mes = "修改";
			}else{
				mes = "新增";
			}
			addActionMessage("Title为" + news.getTitle() + "的新闻"+mes+"附件名为"
					+ attachmention.getUpfileFileName() + "成功Q@!");
		}
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		addActionMessage("Id" + id + "的新闻被删除Q@!");
		messageManager.deleteNews(id);
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			news = messageManager.getNews("id", id);
		} else {
			news = new News();
		}
	}

	public Attachment getAttachmention() {
		return attachmention;
	}

	public void setAttachmention(Attachment attachmention) {
		this.attachmention = attachmention;
	}

	public User getUser() {
		return user;
	}

	public Page<News> getPage() {
		return page;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Autowired
	public void setMessageManager(MessageManager messageManager) {
		this.messageManager = messageManager;
	}


	@Autowired
	public void setSystemManager(SystemManager systemManager) {
		this.systemManager = systemManager;
	}
	
	public String view()throws Exception{
		return VIEW;
	}
	/**
	 * 定义在view()前执行二次绑定.
	 */
	public void prepareView() throws Exception {
		prepareModel();
	}
	@Override
	public News getModel() {
		// TODO Auto-generated method stub
		return news;
	}
	
	
	/**获取在首页显示的新闻列表*/
	public void newsResponse() throws Exception {
		List<News> newsList = new ArrayList<News>();
		newsList = messageManager.getTopNews();
		String root = Struts2Utils.getRootPath();
		String content = "<table class='frontContent'>";
		for(int i = 0; i < newsList.size();i++){
			News newsff = new News();
			newsff = newsList.get(i);
			content += "<tr><td class='front_news'><a href='"+root+"/asset/message/news!view.action?id="+newsff.getId()+"' title='"+newsff.getTitle()+"'>"
			+(i+1)+".&nbsp;"+DateUtils.convertDateToString(newsff.getCreateDate())+"&nbsp;"+newsff.getTitle()+" <SUB>...</SUB></a></td></tr>";
		}
		content += "</table>";
		Struts2Utils.renderText(content);
	}
}
